#include "Main.h"

VOID player_finalize(JSContext *cx, JSObject *obj)
{
	myPlayer* lpPlayer = (myPlayer*)JS_GetPrivate(cx, obj);

	if(lpPlayer)
		delete lpPlayer;
}

INT player_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
	myPlayer* lpPlayer = (myPlayer*)JS_GetPrivate(cx, obj);

	if(!lpPlayer || IsBadReadPtr(lpPlayer, sizeof(myPlayer)) || lpPlayer->_dwPrivateType != PRIVATE_PLAYER)
		return JS_TRUE;

	Player* pPlayer = GetPlayer(lpPlayer->dwPlayerSlot);
	PlayerInfo* pPlayerInfo = GAME_GetPlayerInfo(*p_GAME_GameInfos, NULL, lpPlayer->dwPlayerSlot);
	float fTmp;

	if(!pPlayer || !pPlayerInfo)
		return JS_TRUE;

	switch(JSVAL_TO_INT(id))
	{
		case PLAYER_NAME:
			*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, GAME_GetPlayerName(pPlayer->wPlayerSlot)));
		break;
		case PLAYER_SLOT:
			*vp = INT_TO_JSVAL(pPlayer->wPlayerSlot);
		break;
		case PLAYER_GOLD:
			*vp = INT_TO_JSVAL(GAME_GetGold(pPlayerInfo));
		break;
		case PLAYER_LUMBER:
			*vp = INT_TO_JSVAL(GAME_GetLumber(pPlayerInfo));			
		break;
		case PLAYER_SUPPLY:
			*vp = INT_TO_JSVAL(GAME_GetMinSupply(pPlayerInfo));
		break;
		case PLAYER_SUPPLYMAX:
			*vp = INT_TO_JSVAL(GAME_GetMaxSupply(pPlayerInfo));
		break;
		case PLAYER_RACE:
			*vp = INT_TO_JSVAL(pPlayer->dwRace);
		break;
		case PLAYER_STARTX:
			GetPlayerStartLocationX(pPlayer, &fTmp);
			JS_NewDoubleValue(cx, fTmp, vp);
		break;
		case PLAYER_STARTY:
			GetPlayerStartLocationY(pPlayer, &fTmp);
			JS_NewDoubleValue(cx, fTmp, vp);
		break;
		case ME_MAPNAME:
			*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, (CHAR*)p_GAME_MapName));
		break;
		case ME_MAXPLAYERS:
			*vp = INT_TO_JSVAL((*p_GAME_GameInfos)->dwMaxPlayerCount);
		break;	
		case ME_PLAYERS:
			*vp = INT_TO_JSVAL((*p_GAME_GameInfos)->dwPlayerCount);
		break;
		default:
		break;
	}
	return JS_TRUE;
}

INT player_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
	return JS_TRUE;
}

INT my_getPlayer(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	myPlayer* pPlayer = new myPlayer;

	if(!pPlayer)
		return JS_TRUE;

	memset(pPlayer, NULL, sizeof(myPlayer));
	pPlayer->_dwPrivateType = PRIVATE_PLAYER;

	if(argc > 0)
	{
		if(JSVAL_IS_INT(argv[0]))
		{
			Player* lpPlayer = GetPlayer(JSVAL_TO_INT(argv[0]));
			
			if(!lpPlayer)
			{
				delete pPlayer;
				return JS_TRUE;
			}

			pPlayer->dwPlayerSlot = lpPlayer->wPlayerSlot;
		}
		else if(JSVAL_IS_STRING(argv[0]))
		{
			CHAR* lpszName = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));

			if(!lpszName)
			{
				delete pPlayer;
				return JS_TRUE;
			}

			Player* lpPlayer = GetPlayer(lpszName);

			if(!lpPlayer)
			{
				delete pPlayer;
				return JS_TRUE;
			}

			strcpy(pPlayer->szName, lpszName);
			pPlayer->dwPlayerSlot = lpPlayer->wPlayerSlot;
		}
	}

	JSObject* jsUnit = JS_NewObject(cx, &player_class, NULL, NULL);

	if(!jsUnit)
		return JS_TRUE;

	if(!JS_DefineProperties(cx, jsUnit, player_props))
		return JS_TRUE;
	if(!JS_DefineFunctions(cx, jsUnit, player_methods)) 
		return JS_TRUE;
	
	JS_SetPrivate(cx, jsUnit, pPlayer);

	*rval = OBJECT_TO_JSVAL(jsUnit);
	return JS_TRUE;
}

INT player_getNext(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
	myPlayer* lpPlayer = (myPlayer*)JS_GetPrivate(cx, obj);

	if(!lpPlayer || IsBadReadPtr(lpPlayer, sizeof(myPlayer)) || lpPlayer->_dwPrivateType != PRIVATE_PLAYER)
		return JS_TRUE;

	Player* pPlayer = GetPlayer(lpPlayer->dwPlayerSlot);

	if(!pPlayer)
		return JS_TRUE;

	pPlayer = GetNextPlayer(pPlayer->wPlayerSlot, lpPlayer->szName);

	if(pPlayer)
	{
		lpPlayer->dwPlayerSlot = pPlayer->wPlayerSlot;
		JS_SetPrivate(cx, obj, lpPlayer);
		*rval = OBJECT_TO_JSVAL(obj);
	}
	else
	{
		JS_ClearScope(cx, obj);
		JS_ValueToObject(cx, JSVAL_NULL, &obj);
		*rval = INT_TO_JSVAL(0);
	}
	
	return JS_TRUE;
}